<template>
    <div class="dynamic" @click="operIndex = -1">
        <div class="lemon-sidebar__scroll dynamic_content">
            <div class="title">{{ $t('cir.朋友圈') }}</div>
            <div class="dynamic_view">

                <el-upload :action="upload_url" :show-file-list="false" accept=".jpg,.jpeg,.png,.gif,.bmp,.webp"
                    :before-upload="beforeImageUpload" :on-success="uploadImageSuccess">
                    <div class="dynamic_header">
                        <img :src="background" alt="" class="bg_img">
                        <div class="user_info" @click="router()">
                            <div class="user_name">{{ user.nickname }}</div>
                            <img :src="user.avatar" alt="" class="user_avatar">
                        </div>
                    </div>
                </el-upload>

                <div class="dynamic_list">

                    <div class="new_meg" v-if="unread_count > 0">
                        <div class="new_meg_text" @click="navigateTo('interactive')">
                            {{ unread_count }}{{ $t('cir.条新消息') }}
                        </div>
                    </div>
                    <div class="item" v-for="(item, index) in list" :key="index"
                        @click.stop="router(item.id, 'dynamicDeta')">
                        <div class="item_view">
                            <img :src="item.user_info.avatar" alt="" class="item_avatar" @click.stop="router()"
                                v-if="item.user_id == user.user_id">


                            <img :src="item.user_info.avatar" alt="" class="item_avatar"
                                @click.stop="router(item.user_id, 'singleUserDynamic')" v-else>

                            <div class="item_info">
                                <div class="item_info_name">{{ item.user_info.nickname }}</div>
                                <div class="item_info_value">{{ item.content }}</div>
                                <IndexCover :list="item.images" v-if="item.images.length" />
                                <IndexVideo :src="item.video" v-if="item.video" />
                                <div class="item_info_other">
                                    <div class="item_info_time">{{ item.time_text }}</div>

                                    <div class="item_info_more"
                                        @click.stop="operIndex = operIndex == index ? -1 : index">
                                        <div class="comment_popover" v-if="operIndex == index">
                                            <div class="comment_item" @click.stop="like(item)">
                                                <img src="@/assets/image/d2.png" alt="" class="comment_img"
                                                    v-if="item.is_liked">
                                                <img src="@/assets/image/d1.png" alt="" class="comment_img" v-else>
                                                <div class="comment_text">{{ $t('cir.赞') }}</div>
                                            </div>
                                            <div class="comment_item" @click.stop="router(item.id, 'dynamicDeta')">
                                                <img src="@/assets/image/d3.png" alt="" class="comment_img">
                                                <div class="comment_text">{{ $t('cir.评论') }}</div>
                                            </div>
                                        </div>
                                        <i class="el-icon-more"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="like_list" v-if="item.like_count > 0">
                            <img src="@/assets/image/d4.png" alt="" class="like_img">
                            <div class="like_view">
                                <span class="like_name" v-for="(likeItem, likeIndex) in item.like_users"
                                    :key="likeIndex">
                                    {{ likeItem.nickname }}
                                    <span v-if="likeIndex != (item.like_count - 1)">，</span>
                                </span>
                            </div>
                        </div>

                        <div class="comment_list" v-if="item.comment_count > 0">
                            <div class="comment_view" v-for="(dataItem, dataIndex) in item.comments" :key="dataIndex">
                                <span class="comment_name" v-if="!dataItem.reply_user">{{ dataItem.nickname }}：</span>
                                <span v-else>
                                    <span class="comment_name">{{ dataItem.nickname }}</span>
                                    <span class="comment_reply">{{ $t('cir.回复') }}</span>
                                    <span class="comment_name">{{ dataItem.reply_user.nickname }}：</span>
                                </span>
                                <span class="comment_msg" v-html="emojiToHtml(dataItem.content)"></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="page_view">
                <el-pagination background layout="prev, pager, next" hide-on-single-page :current-page="page"
                    :total="total" @current-change="change" />
            </div>
        </div>
    </div>
</template>
<script>
import {
    mapState
} from 'vuex';
import IndexCover from "@/components/my/IndexCover.vue"
import IndexVideo from "@/components/my/IndexVideo.vue"

import emoji from '@/utils/emoji.js'
export default {
    components: {
        IndexCover,
        IndexVideo
    },
    computed: {
        ...mapState({
            user: state => state.user,
            unread_count: state => state.unread_count
        })
    },
    data() {
        return {
            upload_url: window.BASE_URL + 'common/upload',
            background: "",
            operIndex: -1,
            page: 1,
            list: [],
            total: 0,
        }
    },
    created() {
        let emojiMap = [];
        emoji.forEach(function (item) {
            let child = item.children;
            if (child.length > 0) {
                child.forEach(function (val) {
                    let name = val.name;
                    let src = val.src;
                    emojiMap[name] = src;
                })
            }
        });
        this.emojiMap = emojiMap;
        this.getList()
    },

    methods: {
        router(id, name) {
            if (!id) {
                this.$router.push({
                    name: 'myDynamic'
                })
                return
            }
            this.$router.push({
                name: name,
                query: { id }
            })
        },
        navigateTo(name) {
            this.$router.push({
                name
            })
        },
        // 封面上传前验证（仅图片，最大2M）
        beforeImageUpload(file) {
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isLt2M) {
                this.$message.error(this.$t('mall.最大') + '2MB!');
                return false;
            }
            return true;
        },
        // 封面上传成功回调
        async uploadImageSuccess(response) {
            if (response && response.data && response.data.fullurl) {
                this.background = response.data.fullurl;

                await this.$api.momentApi.setBgImage({
                    bg_image: response.data.fullurl
                });
            }
        },

        async getList() {
            let res = await this.$api.momentApi.momentList({
                page: this.page,
                page_size: 10
            });
            let list = res.list;
            this.list = list.data;
            this.total = list.total;
            if (this.page == 1) this.background = res.moment_bg_image;
        },
        change(value) {
            this.page = value;
            this.getList();
        },

        async like(item) {
            if (item.is_liked) {
                let index = item.like_users.findIndex(user => user.user_id == this.user.user_id);

                await this.$api.momentApi.likeDelete({
                    moment_id: item.id
                });
                item.like_users.splice(index, 1);
                item.like_count -= 1;
            } else {
                await this.$api.momentApi.like({
                    moment_id: item.id
                });

                item.like_users.push({
                    user_id: this.user.user_id,
                    nickname: this.user.nickname
                })
                item.like_count += 1;
            }
            item.is_liked = !item.is_liked;
        },

        emojiToHtml(str) {
            let emojiMap = this.emojiMap;
            str = str.replace(/\[!(\w+)\]/gi, function (str, match) {
                var file = match;
                return emojiMap[file] ?
                    "<img style=\"width:12px;height:12px;margin:0 2px;\" emoji-name=\""
                        .concat(match, "\" src=\"").concat(emojiMap[file], "\" />") : "[!".concat(match, "]");
            });
            str = '<span class="rich_text">' + str + '</span>';
            return str;
        }
    }
}
</script>
<style lang="scss" scoped>
.rich_text {
    display: flex;
    align-items: center;
}

.dynamic {
    min-width: 100%;
    height: 100%;
    border-radius: 10px 10px 0 0;
    padding: 24px;
    box-sizing: border-box;
    background: #FCFCFE;
    position: relative;
    overflow: hidden;

    .dynamic_content {
        height: 100%;
        overflow-y: auto;

        .title {
            font-weight: bold;
            font-size: 20px;
            color: #333333;
            padding-bottom: 16px;
        }
    }



    .dynamic_view {
        width: 544px;
        margin: 0 auto;
        background: #FFFFFF;
        box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
        border-radius: 4px 4px 4px 4px;

        .dynamic_header {
            width: 544px;
            height: 330px;
            position: relative;

            .bg_img {
                width: 100%;
                height: 100%;
            }

            .user_info {
                position: absolute;
                right: 16px;
                bottom: -12px;
                z-index: 2;
                display: flex;
                align-items: center;

                .user_name {
                    font-size: 14px;
                    color: #FFFFFF;
                    padding-right: 8px;
                }

                .user_avatar {
                    width: 48px;
                    height: 48px;
                    border-radius: 6px 6px 6px 6px;
                }
            }
        }

        .dynamic_list {
            padding: 24px 16px;
            box-sizing: border-box;

            .new_meg {
                padding-bottom: 10px;
                display: flex;
                justify-content: center;

                .new_meg_text {
                    min-width: 110px;
                    height: 35px;
                    line-height: 35px;
                    text-align: center;
                    color: #fff;
                    border-radius: 8px;
                    padding: 0 5px;
                    background-color: rgba(0, 0, 0, .5);
                    cursor: pointer;
                }
            }

            .item {
                border-bottom: 1px solid #EDEDED;
                padding-bottom: 16px;
                padding-top: 24px;

                &_view {
                    display: flex;
                    justify-content: space-between;
                }

                .like_list {
                    border-bottom: 1px solid #EDEDED;
                    padding: 4px 8px;
                    box-sizing: border-box;
                    background: #F7F7F7;
                    display: flex;
                    align-items: center;

                    .like_img {
                        width: 12px;
                        height: 11px;
                    }

                    .like_view {
                        flex: 1;
                        padding-left: 3px;
                        box-sizing: border-box;

                        .like_name {
                            color: #4A5F7C;
                            font-size: 12px;
                        }
                    }
                }

                .comment_list {
                    padding: 4px 8px;
                    box-sizing: border-box;
                    background: #F7F7F7;
                    cursor: default;

                    .comment_view {
                        padding: 2px 0;
                    }

                    .comment_name {
                        font-size: 12px;
                        color: #4A5F7C;
                        line-height: 18px;
                    }

                    .comment_reply {
                        font-size: 12px;
                        color: #333;
                        padding: 0 5px;
                        box-sizing: border-box;
                    }

                    .comment_msg {
                        font-size: 12px;
                        color: #333;

                        :deep(img) {
                            vertical-align: middle;
                        }
                    }
                }

                &_avatar {
                    width: 36px;
                    height: 36px;
                    border-radius: 6px 6px 6px 6px;
                }

                &_info {
                    flex: 1;
                    padding: 0 8px;
                    box-sizing: border-box;

                    &_name {
                        font-weight: bold;
                        font-size: 14px;
                        color: #4A5F7C;
                    }

                    &_value {
                        font-size: 14px;
                        color: #333333;
                        margin: 8px 0;
                    }

                    &_other {
                        height: 44px;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        margin-top: 8px;
                    }

                    &_time {
                        font-size: 14px;
                        color: #9A9A9A;
                    }

                    &_more {
                        font-size: 20px;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        .comment_popover {
                            display: flex;
                            height: 44px;
                            border-radius: 6px 6px 6px 6px;
                            margin-right: 12px;
                            background: #4C4C4C;

                            .comment_item {
                                padding: 0 10px;
                                display: flex;
                                align-items: center;
                                justify-content: center;
                                cursor: pointer;
                                position: relative;

                                &:first-child::after {
                                    content: "";
                                    height: 19px;
                                    border: 1px solid #929292;
                                    position: absolute;
                                    right: 0;
                                    top: 50%;
                                    transform: translateY(-50%);
                                }

                                .comment_img {
                                    width: 18px;
                                    height: 18px;
                                }

                                .comment_text {
                                    line-height: 14px;
                                    font-size: 14px;
                                    color: #FFFFFF;
                                    margin-left: 4px;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>
